package com.atguigu.daijia.customer.service.impl;

import com.atguigu.daijia.common.constant.RedisConstant;
import com.atguigu.daijia.common.execption.GuiguException;
import com.atguigu.daijia.common.result.Result;
import com.atguigu.daijia.common.result.ResultCodeEnum;
import com.atguigu.daijia.customer.client.CustomerInfoFeignClient;
import com.atguigu.daijia.customer.service.CustomerService;
import com.atguigu.daijia.model.vo.customer.CustomerLoginVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Slf4j
@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    private CustomerInfoFeignClient client;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Override
    public String login(String code) {
        //远程调用，返回用户id
        Result<Long> lginResult = client.login(code);
        //判断返回是否成功
        Integer codeResult = lginResult.getCode();
        if (codeResult != 200){
            throw new GuiguException(ResultCodeEnum.DATA_ERROR);
        }
        //获取用户id
        Long userId = lginResult.getData();
        //判断id是否为空
        if (userId == null){
            throw new GuiguException(ResultCodeEnum.DATA_ERROR);
        }
        //生成token字符串
        String token = UUID.randomUUID().toString().replaceAll("-","");
        //把用户id放到redis存储，设置过期时间
        stringRedisTemplate.opsForValue().set(RedisConstant.USER_LOGIN_KEY_PREFIX + token,userId.toString(),RedisConstant.USER_LOGIN_KEY_TIMEOUT, TimeUnit.SECONDS);
        //返回token
        return token;
    }

    /*@Override
    public CustomerLoginVo getCustomerLoginInfo(String token) {
        //获取请求头中的token
        //查询redis
        String key = RedisConstant.USER_LOGIN_KEY_PREFIX + token;

        //查询token对应的用户id
        String userId = stringRedisTemplate.opsForValue().get(key);
        if (StringUtils.isEmpty(userId)){
            throw new GuiguException(ResultCodeEnum.DATA_ERROR);
        }
        //获取用户信息
        Result<CustomerLoginVo> customerInfo = client.getCustomerInfo(Long.valueOf(userId));
        Integer code = customerInfo.getCode();
        if (code != 200){
            throw new GuiguException(ResultCodeEnum.DATA_ERROR);
        }
        CustomerLoginVo customerLoginVo = customerInfo.getData();
        if (customerLoginVo == null){
            throw new GuiguException(ResultCodeEnum.DATA_ERROR);
        }
        return customerLoginVo;
    }*/

    @Override
    public CustomerLoginVo getCustomerInfo(Long userId) {
        //获取用户信息
        Result<CustomerLoginVo> customerInfo = client.getCustomerInfo(userId);
        Integer code = customerInfo.getCode();
        if (code != 200){
            throw new GuiguException(ResultCodeEnum.DATA_ERROR);
        }
        CustomerLoginVo customerLoginVo = customerInfo.getData();
        if (customerLoginVo == null){
            throw new GuiguException(ResultCodeEnum.DATA_ERROR);
        }
        return customerLoginVo;
    }
}
